package inet.app.vod.util;

import inet.app.vod.object.FunModel;
import inet.app.vod.object.SubtitleSettingModel;
import inet.app.vod.object.User;
import inet.app.vod.video.MyMediaControllerBase;
import inet.app.vod.video.MyVideoView;

import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.inet.app.framework.memory.MyPreference;
import com.inet.app.framework.util.JsonUtil;
import com.inet.app.framework.util.Log;

public class MyPreferenceManager {

	public static final String VOLUME = "volume";
	public static final int VOLUME_DEFAULT = 50;

	public static final String BRIGHTNESS = "brightness";
	public static final int BRIGHTNESS_DEFAULT = -1;

	public static final String SCALE = "scale";
	public static final int SCALE_DEFAULT = MyVideoView.VIDEO_LAYOUT_STRETCH;

	public static final String TYPE_CONTROLLER = "type_controller";
	public static final int TYPE_CONTROLLER_DEFAULT = MyMediaControllerBase.TYPE_BUTTON;

	public static final String SHOW_RATE_APP = "rate_app";
	public static final boolean SHOW_RATE_APP_DEFAULT = true;

	public static final String LAST_DATE_SHOW_DIALOG_RATE_APP = "date_rate_app";

	public static final String IS_FIRST_RATE_APP = "first_rate_app";
	public static final boolean IS_FIRST_RATE_APP_DEFAULT = true;

	public static final String SUBTITLE_SETTING_FONT = "subtitle_font";
	public static final String SUBTITLE_SETTING_SIZE = "subtitle_size";
	public static final String SUBTITLE_SETTING_COLOR = "subtitle_color";
	public static final String SUBTITLE_SETTING_BOLD = "subtitle_bold";

	public static final String USER_NAME_RANDOM = "user_name_random";
	public static final String PASS_RANDOM = "pass_random";

	public static final String ROOT_TOKEN = "root_token";

	private static MyPreferenceManager instance;
	private MyPreference preference;

	private MyPreferenceManager(Context context) {
		this.preference = MyPreference.getInstance(context);
	}

	public static MyPreferenceManager getInstance(Context context) {
		if (instance == null) {
			instance = new MyPreferenceManager(context);
		}
		return instance;
	}

	/**
	 * clear values
	 */
	public void clear() {
		preference.clear();
	}

	/**
	 * Save volume
	 */
	public void setVolume(int volume) {
		preference.putInt(VOLUME, volume);
	}

	/**
	 * Get volume
	 */
	public int getVolume() {
		return preference.getInt(VOLUME, VOLUME_DEFAULT);
	}

	/**
	 * Save brightness
	 */
	public void setBrightness(int brightness) {
		preference.putInt(BRIGHTNESS, brightness);
	}

	/**
	 * Get brightness
	 */
	public int getBrightness() {
		return preference.getInt(BRIGHTNESS, BRIGHTNESS_DEFAULT);
	}

	/**
	 * Save type scale
	 */
	public void setScale(int scale) {
		preference.putInt(SCALE, scale);
	}

	/**
	 * Get type scale
	 */
	public int getScale() {
		return preference.getInt(SCALE, SCALE_DEFAULT);
	}

	/**
	 * Save type controller
	 */
	public void setTypeMediaController(int type) {
		preference.putInt(TYPE_CONTROLLER, type);
	}

	/**
	 * Get type controller
	 */
	public int getTypeController() {
		return preference.getInt(TYPE_CONTROLLER, TYPE_CONTROLLER_DEFAULT);
	}

	/**
	 * Save true/false show dialog rate app
	 */
	public void setShowDialogRateApp(boolean isShow) {
		preference.putBoolean(SHOW_RATE_APP, isShow);
	}

	/**
	 * Get true/false show dialog rate app
	 */
	public boolean getShowDialogRateApp() {
		return preference.getBoolean(SHOW_RATE_APP, SHOW_RATE_APP_DEFAULT);
	}

	/**
	 * Save last date show dialog rate app
	 */
	public void setLastDateShowDialogRateapp(Date date) {
		preference.putLong(LAST_DATE_SHOW_DIALOG_RATE_APP, date.getTime());
	}

	/**
	 * Get last date show dialog rate app
	 */
	public Date getLastDateShowDialogRateapp() {
		// value default return is current date
		Date date = new Date();
		long millis = preference.getLong(LAST_DATE_SHOW_DIALOG_RATE_APP,
				date.getTime());
		return new Date(millis);
	}

	/**
	 * Save first rate app
	 */
	public void setFirstRateApp(boolean isFirst) {
		preference.putBoolean(IS_FIRST_RATE_APP, isFirst);
	}

	/**
	 * Get first rate app
	 */
	public boolean getFirseRateAp() {
		return preference.getBoolean(IS_FIRST_RATE_APP,
				IS_FIRST_RATE_APP_DEFAULT);
	}

	/**
	 * Save subtitle setting
	 */
	public void setSubtileSetting(SubtitleSettingModel model) {
		preference.putString(SUBTITLE_SETTING_FONT, model.getFont());
		preference.putInt(SUBTITLE_SETTING_SIZE, model.getSize());
		preference.putInt(SUBTITLE_SETTING_COLOR, model.getColor());
		preference.putBoolean(SUBTITLE_SETTING_BOLD, model.isBold());
	}

	/**
	 * Get subtitle setting
	 */
	public SubtitleSettingModel getSubtitleSetting() {

		SubtitleSettingModel modelDefault = new SubtitleSettingModel();

		modelDefault.setFont(preference.getString(SUBTITLE_SETTING_FONT,
				modelDefault.getFont()));
		modelDefault.setSize(preference.getInt(SUBTITLE_SETTING_SIZE,
				modelDefault.getSize()));
		modelDefault.setColor(preference.getInt(SUBTITLE_SETTING_COLOR,
				modelDefault.getColor()));
		modelDefault.setBold(preference.getBoolean(SUBTITLE_SETTING_BOLD,
				modelDefault.isBold()));

		return modelDefault;
	}

	/**
	 * Save account name and password
	 */
	public void setAccount(User account) {
		String jUser = new Gson().toJson(account);
		preference.putString("account", jUser);
	}

	/**
	 * Get account info
	 */
	public User getAccount() {
		String jUser = preference.getString("account", "");
		User account = null;
		try {
			account = new Gson().fromJson(jUser, User.class);
		} catch (Exception e) {
			Log.e(e.toString());
			return null;
		}
		return account;
	}

	/**
	 * Logout, clear account name and password
	 */
	public void clearAccount() {
		preference.putString("account", null);
	}

	public void saveFuns(List<FunModel> funs) {
		String jFuns = JsonUtil.getJsonFromList(funs);
		preference.putString("funs", jFuns);
	}

	public List<FunModel> getFuns() {
		String jFuns = preference.getString("funs", null);
		if (TextUtils.isEmpty(jFuns)) {
			return null;
		}
		List<FunModel> funs = (List<FunModel>) JsonUtil.getListFromJson(jFuns);
		return funs;
	}

	public void setUserRandom(String userName) {
		preference.putString(USER_NAME_RANDOM, userName);
	}

	public String getUserRandom() {
		return preference.getString(USER_NAME_RANDOM, "");
	}

	public void setPassRandom(String pass) {
		preference.putString(PASS_RANDOM, pass);
	}

	public String getPassRandom() {
		return preference.getString(PASS_RANDOM, "");
	}

	public void saveRootToken(String rootToken) {
		preference.putString(ROOT_TOKEN, rootToken);
	}

	public String getRootToken() {
		return preference.getString(ROOT_TOKEN, "");
	}

}
